

factorAsNumbers <- function(fac){ 
  uniquefac <- sort(unique(fac),na.last=T)
  new <- rep(NA,length(fac))
  for (i in 1:length(uniquefac)){
    new[fac==uniquefac[i]] <- i
  }
  if (NA %in% new){
    new[is.na(new)] <- which(is.na(uniquefac)) #<----- ?
  }
  return(new)
}

factorAsNumbersNoNA <- function(fac){ 
  uniquefac <- sort(unique(fac))
  new <- rep(NA,length(fac))
  for (i in 1:length(uniquefac)){
    new[fac==uniquefac[i]] <- i
  }
  return(new)
}

lengthunique <- function(x,no.na=F){
  z <- unique(x)
  if(no.na){z <- unique(x[! is.na(x)])}
  return(length(z))}

prepareForGpc <- function(data,no.na=F){
  n <- dim(data)[1]
  m <- dim(data)[2]
  y <- matrix(NA,dim(data)[1],dim(data)[2])
  for (i in 1:dim(y)[2]){
    if(! no.na){y[,i] <- factorAsNumbers(data[,i])}
    if(no.na){y[,i] <- factorAsNumbersNoNA(data[,i])}
  }
  #y <- apply(data,2,factorAsNumbers) # for some reason this screws up the ordering
  #if(no.na){y <- apply(data,2,factorAsNumbersNoNA)}
  K <- apply(data,2,lengthunique,no.na)
  return(list(y=y,K=K,n=n,m=m))
}

